DATESTAMP ?= $(shell date '+%Y%m%d')
PWD ?= $(shell pwd)

# whether or not to build universal
export DO_UNIVERSAL = 1

# whether or not to package .dmg files
export MAKE_DMGS = 1

# whether or not to do a clean build
export DO_CLEAN = 0

# whether or not to update svn when building
export DO_UPDATE = 1

# whether or not to build from source tarballs, if they exist in sources/
export USE_SOURCES = 0

MAKETORRENT=/sw/bin/btmaketorrent.py
PACKAGEMAKER := /Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker

# kdebindings
DAILY := kdesupport kdelibs kdepimlibs kdebase kdebase-runtime amarok kdeadmin kdeartwork kdeedu kdegames kdegraphics kdemultimedia kdenetwork kdepim kdesdk kdetoys kdeutils kdevplatform kdevelop kdewebdev koffice amarok ktorrent

MANUAL := qt qt-doc qt-extras kdeprereqs

PACKAGES           := $(DAILY)

DAILY_TORRENT      := $(patsubst %,kde-mac/%-$(DATESTAMP).torrent,$(DAILY))
DAILY_DMG          := $(patsubst %,kde-mac/%-$(DATESTAMP).dmg,$(DAILY))
DAILY_PKG          := $(patsubst %,kde-mac/%.pkg,$(DAILY))

MANUAL_TORRENT     := $(patsubst %,kde-mac/%-$(DATESTAMP).torrent,$(MANUAL))
MANUAL_DMG         := $(patsubst %,kde-mac/%-$(DATESTAMP).dmg,$(MANUAL))
MANUAL_PKG         := $(patsubst %,kde-mac/%.pkg,$(MANUAL))

EVERYTHING_TORRENT := kde-mac/everything-$(DATESTAMP).torrent
EVERYTHING_DMG     := kde-mac/everything-$(DATESTAMP).dmg
EVERYTHING_PKG     := $(DAILY_PKG) $(MANUAL_PKG) kde-mac/kde.mpkg

EVERYTHING         := $(EVERYTHING_TORRENT)
FAKE_TORRENT       := $(patsubst %,kde-mac/%.torrent,$(DAILY) $(MANUAL))

.PHONY: $(FAKE_TORRENT)

.PHONY: all
all:
	@echo "usage: make <clean|dist>"
	@echo ""

.PHONY: clean
clean:
	@rm -rf kde-mac/*.pkg kde-mac/*.mpkg kde-mac/*.dmg kde-mac/*.torrent

.PHONY: cleandist
cleandist:
	@./make-packages.sh --clean $(DIST_ARGS) $(PACKAGES)
	$(MAKE) $(EVERYTHING)

.PHONY: distnoupdate
distnoupdate:
	@./make-packages.sh --skip-update $(DIST_ARGS) $(PACKAGES)
	$(MAKE) $(EVERYTHING)

.PHONY: dist
dist:
	@./make-packages.sh $(DIST_ARGS) $(PACKAGES)
	$(MAKE) $(EVERYTHING)

.PHONY: dmgs
dmgs:	$(DAILY_DMG)

.PHONY: torrents
torrents: $(DAILY_TORRENT) $(EVERYTHING_TORRENT)

# dang, I can't figure out how to make these automatically, stupid make
kde-mac/qt.torrent: kde-mac/qt-$(DATESTAMP).torrent
kde-mac/qt.dmg: kde-mac/qt-$(DATESTAMP).dmg
kde-mac/qt-doc.torrent: kde-mac/qt-doc-$(DATESTAMP).torrent
kde-mac/qt-doc.dmg: kde-mac/qt-doc-$(DATESTAMP).dmg
kde-mac/qt-extras.torrent: kde-mac/qt-extras-$(DATESTAMP).torrent
kde-mac/qt-extras.dmg: kde-mac/qt-extras-$(DATESTAMP).dmg
kde-mac/kdeprereqs.torrent: kde-mac/kdeprereqs-$(DATESTAMP).torrent
kde-mac/kdeprereqs.dmg: kde-mac/kdeprereqs-$(DATESTAMP).dmg
kde-mac/kdesupport.torrent: kde-mac/kdesupport-$(DATESTAMP).torrent
kde-mac/kdesupport.dmg: kde-mac/kdesupport-$(DATESTAMP).dmg
kde-mac/everything.torrent: kde-mac/everything-$(DATESTAMP).torrent
kde-mac/everything.dmg: kde-mac/everything-$(DATESTAMP).dmg
kde-mac/decibel.torrent: kde-mac/decibel-$(DATESTAMP).torrent
kde-mac/decibel.dmg: kde-mac/decibel-$(DATESTAMP).dmg
kde-mac/kdelibs.torrent: kde-mac/kdelibs-$(DATESTAMP).torrent
kde-mac/kdelibs.dmg: kde-mac/kdelibs-$(DATESTAMP).dmg
kde-mac/kdepimlibs.torrent: kde-mac/kdepimlibs-$(DATESTAMP).torrent
kde-mac/kdepimlibs.dmg: kde-mac/kdepimlibs-$(DATESTAMP).dmg
kde-mac/kdebase.torrent: kde-mac/kdebase-$(DATESTAMP).torrent
kde-mac/kdebase.dmg: kde-mac/kdebase-$(DATESTAMP).dmg
kde-mac/kdebase-runtime.torrent: kde-mac/kdebase-runtime-$(DATESTAMP).torrent
kde-mac/kdebase-runtime.dmg: kde-mac/kdebase-runtime-$(DATESTAMP).dmg
kde-mac/amarok.torrent: kde-mac/amarok-$(DATESTAMP).torrent
kde-mac/amarok.dmg: kde-mac/amarok-$(DATESTAMP).dmg
kde-mac/kdeadmin.torrent: kde-mac/kdeadmin-$(DATESTAMP).torrent
kde-mac/kdeadmin.dmg: kde-mac/kdeadmin-$(DATESTAMP).dmg
kde-mac/kdeartwork.torrent: kde-mac/kdeartwork-$(DATESTAMP).torrent
kde-mac/kdeartwork.dmg: kde-mac/kdeartwork-$(DATESTAMP).dmg
kde-mac/kdebindings.torrent: kde-mac/kdebindings-$(DATESTAMP).torrent
kde-mac/kdebindings.dmg: kde-mac/kdebindings-$(DATESTAMP).dmg
kde-mac/kdeedu.torrent: kde-mac/kdeedu-$(DATESTAMP).torrent
kde-mac/kdeedu.dmg: kde-mac/kdeedu-$(DATESTAMP).dmg
kde-mac/kdegames.torrent: kde-mac/kdegames-$(DATESTAMP).torrent
kde-mac/kdegames.dmg: kde-mac/kdegames-$(DATESTAMP).dmg
kde-mac/kdegraphics.torrent: kde-mac/kdegraphics-$(DATESTAMP).torrent
kde-mac/kdegraphics.dmg: kde-mac/kdegraphics-$(DATESTAMP).dmg
kde-mac/kdemultimedia.torrent: kde-mac/kdemultimedia-$(DATESTAMP).torrent
kde-mac/kdemultimedia.dmg: kde-mac/kdemultimedia-$(DATESTAMP).dmg
kde-mac/kdenetwork.torrent: kde-mac/kdenetwork-$(DATESTAMP).torrent
kde-mac/kdenetwork.dmg: kde-mac/kdenetwork-$(DATESTAMP).dmg
kde-mac/kdepim.torrent: kde-mac/kdepim-$(DATESTAMP).torrent
kde-mac/kdepim.dmg: kde-mac/kdepim-$(DATESTAMP).dmg
kde-mac/kdesdk.torrent: kde-mac/kdesdk-$(DATESTAMP).torrent
kde-mac/kdesdk.dmg: kde-mac/kdesdk-$(DATESTAMP).dmg
kde-mac/kdetoys.torrent: kde-mac/kdetoys-$(DATESTAMP).torrent
kde-mac/kdetoys.dmg: kde-mac/kdetoys-$(DATESTAMP).dmg
kde-mac/kdeutils.torrent: kde-mac/kdeutils-$(DATESTAMP).torrent
kde-mac/kdeutils.dmg: kde-mac/kdeutils-$(DATESTAMP).dmg
kde-mac/kdewebdev.torrent: kde-mac/kdewebdev-$(DATESTAMP).torrent
kde-mac/kdewebdev.dmg: kde-mac/kdewebdev-$(DATESTAMP).dmg
kde-mac/kdevplatform.torrent: kde-mac/kdevplatform-$(DATESTAMP).torrent
kde-mac/kdevplatform.dmg: kde-mac/kdevplatform-$(DATESTAMP).dmg
kde-mac/kdevelop.torrent: kde-mac/kdevelop-$(DATESTAMP).torrent
kde-mac/kdevelop.dmg: kde-mac/kdevelop-$(DATESTAMP).dmg
kde-mac/koffice.torrent: kde-mac/koffice-$(DATESTAMP).torrent
kde-mac/koffice.dmg: kde-mac/koffice-$(DATESTAMP).dmg
kde-mac/phonon.torrent: kde-mac/phonon-$(DATESTAMP).torrent
kde-mac/phonon.dmg: kde-mac/phonon-$(DATESTAMP).dmg
kde-mac/ktorrent.torrent: kde-mac/ktorrent-$(DATESTAMP).torrent
kde-mac/ktorrent.dmg: kde-mac/ktorrent-$(DATESTAMP).dmg

kde-mac/everything-$(DATESTAMP).dmg: $(EVERYTHING_PKG)
	@/bin/sh $(PWD)/pkg-to-dmg.sh "$@" kde-mac/*.pkg kde-mac/*.mpkg

kde-mac/%-$(DATESTAMP).dmg: kde-mac/%.pkg
	@/bin/sh $(PWD)/pkg-to-dmg.sh "$@" "$<"

kde-mac/%-$(DATESTAMP).dmg.torrent: kde-mac/%-$(DATESTAMP).dmg
	@echo "making torrent for $<"
	@cd kde-mac; $(MAKETORRENT) http://server2.befunk.com:8080/announce `basename "$<"`; cd -

kde-mac/%-$(DATESTAMP).torrent: kde-mac/%.pkg
	@echo "making torrent for $<"
	@cd kde-mac && \
		mkdir -p kde-mac && \
		rsync -avr --delete ../"$<" kde-mac/ && \
		$(MAKETORRENT) --comment "KDE for Mac OS X: $<" --target ../"$@" http://server2.befunk.com:8080/announce kde-mac && \
		rm -rf kde-mac/*; \
		cd -

kde-mac/everything-$(DATESTAMP).torrent: $(EVERYTHING_PKG)
	@echo "making torrent for $<"
	@cd kde-mac && \
		mkdir -p kde-mac && \
		rsync -avr --delete *.pkg *.mpkg kde-mac/ && \
		$(MAKETORRENT) --comment "KDE for Mac OS X: everything" --target ../"$@" http://server2.befunk.com:8080/announce kde-mac && \
		rm -rf kde-mac/*; \
		cd -

kde-mac/kdeprereqs-$(DATESTAMP).pkg: kdeprereqs.pmproj
	@/bin/sh $(PWD)/pmproj-to-pkg.sh -r "$<" "$@"

kde-mac/kdesupport-$(DATESTAMP).pkg: kdesupport.pmproj
	@/bin/sh $(PWD)/pmproj-to-pkg.sh -r "$<" "$@"

kde-mac/%.mpkg kde-mac/%.pkg: %.pmproj
	@/bin/sh $(PWD)/pmproj-to-pkg.sh "$<" "$@"
	@touch "$@"

